import 'package:flutter/material.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';

import '../controller/base_controller.dart';
import '../controller/base_list_controller.dart';
import '../refresh/base_refresh_configuration.dart';
import 'base_get_builder.dart';

class BaseListGetBuilder<T extends BaseListController> extends BaseGetBuilder<T> {
  const BaseListGetBuilder({
    super.key,
    super.autoRemove = true,
    super.id,
    super.tag,
    super.builder,
    super.initMethod,
    super.global = true,
    super.assignId = false,
    super.filter,
    super.initState,
    super.dispose,
    super.didChangeDependencies,
    super.didUpdateWidget,
    super.init,
    super.backgroundView,
    super.appBar,
    super.backgroundColor,
    super.canPop,
    super.errorBuilder,
    super.emptyBuilder,
    super.clickEmptyOrError,
    super.noNetworkBuilder,
    super.loadingBuilder,
    super.isShowLoading = true,
    super.extendBodyBehindAppBar,
    super.resizeToAvoidBottomInset,
    super.bodyTopViewBuilder,
    super.bodyBottomViewBuilder,
    super.useScaffold,
    // super.isCoverBodyTop,
    super.isCoverBodyTopPadding,
    super.drawer,
    super.endDrawer,
    this.tapEmptyShowLoading = false,
    this.enablePullDown = true,
    this.enablePullUp = false,
    this.onLoading,
    this.onRefresh,
    this.refreshHeader,
    this.refreshFooter,
    this.headerBuilder,
    this.footerBuilder,
    this.initialRefresh = true,
  });

  final bool tapEmptyShowLoading;
  final bool enablePullUp;
  final bool enablePullDown;
  final VoidCallback? onLoading;
  final VoidCallback? onRefresh;
  final Widget? refreshHeader;
  final Widget? refreshFooter;
  final Widget Function(T controller)? headerBuilder;
  final Widget Function(T controller)? footerBuilder;
  final bool initialRefresh;

  @override
  void doInitState() {
    controller?.initialRefresh = initialRefresh;
    super.doInitState();
  }

  @override
  Widget buildTopLevelContent(T controller) {
    var widget = builder == null ? buildListView(controller) : builder!.call(controller);
    if ((headerBuilder != null || footerBuilder != null) && widget is! RenderObjectWidget) {
      throw UnimplementedError("当前包含头或底，请使用SliverGrid或SliverList");
    } else {
      return widget;
    }
  }

  @override
  Widget createBuilder(T c) {
    return BaseRefreshConfiguration(
      child: SmartRefresher(
        key: key,
        header: refreshHeader,
        footer: refreshFooter,
        controller: controller!.refreshController,
        onRefresh: onRefresh ?? controller?.refreshList,
        onLoading: onLoading,
        enablePullUp: enablePullUp,
        enablePullDown: enablePullDown,
        child: headerBuilder != null || footerBuilder != null
            ? CustomScrollView(
                slivers: [
                  if (headerBuilder != null) SliverToBoxAdapter(child: headerBuilder?.call(c)),
                  if (isShowLoading == false ||
                      c.viewStatus == ViewStatus.finished ||
                      c.viewStatus == ViewStatus.unknown)
                    super.createBuilder(c)
                  else
                    SliverToBoxAdapter(
                      child: super.createBuilder(c),
                    ),
                  if (footerBuilder != null) SliverToBoxAdapter(child: footerBuilder?.call(c)),
                ],
              )
            : super.createBuilder(c),
      ),
    );
  }

  ScrollView buildListView(T controller) {
    // TODO: implement buildListView
    throw UnimplementedError("buildListView 必须实现");
  }

  @override
  void clickToRefresh() {
    if (this.tapEmptyShowLoading) {
      controller?.viewStatus = ViewStatus.loading;
    }
    controller?.refreshList();
  }
}
